package com.jameci.luntan.controller;

import com.jameci.luntan.dto.PaginationDTO;
import com.jameci.luntan.model.User;
import com.jameci.luntan.service.PostService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ProfileController {
    @Autowired
    private PostService postService;
    @GetMapping("/profile/{action}")
    public String profile(
            HttpServletRequest request,
            @PathVariable(name = "action") String action,
            Model model,
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "5") int size
    ) {
        if(action.equals("posts")) {
            User user = (User) request.getSession().getAttribute("user");
            if(user == null) {
                return "index";
            }else {
                model.addAttribute("action", "posts");
                model.addAttribute("name", "我的发布");
                PaginationDTO paginationDTO = postService.list(page, size, user.getId());
                model.addAttribute("paginationDTO", paginationDTO);
            }
        }else if(action.equals("reception")) {
            model.addAttribute("action", "reception");
            model.addAttribute("name", "最新回复");
        }
        return "profile";
    }
}
